package com.tech.distribute_id.db;

import com.tech.distribute_id.AbstractDistributeIdStrategy;
import com.tech.distribute_id.StrategyTypeEnum;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * @author litianlin
 * @date 2023/11/23 18:10
 * @description:
 * 数据库方案
 *  自增主键。
 *  id记录表。
 *
 * 建表
    CREATE TABLE `distribute_id_info` (
    `id` bigint NOT NULL AUTO_INCREMENT,
    `type` varchar(10) NOT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='全局id表';
 *
 */
@Component
public class DbDistributeIdStrategy extends AbstractDistributeIdStrategy {

    @Resource
    private DistributeIdInfoDOMapper mapper;

    @Override
    public StrategyTypeEnum getStrategyType() {
        return StrategyTypeEnum.DB;
    }

    @Override
    public String getDistributeId() {
        DistributeIdInfoDO item = new DistributeIdInfoDO();
        item.setType("a");
        mapper.insert(item);
        return item.getId().toString();
    }

    public String getDistributeId2() {
        DistributeIdInfoDO item = new DistributeIdInfoDO();
        item.setType("a");
        DistributeIdInfoDO item2 = new DistributeIdInfoDO();
        item2.setType("a");
        mapper.insertList(Arrays.asList(item, item2));
        return item.getId().toString();
    }

}
